package com.xykj.deploy.host.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xykj.deploy.host.entity.Host;
import com.xykj.deploy.host.service.IHostService;
import com.xykj.deploy.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description: 主机表
 * @Author: jeecg-boot
 * @Date: 2024-11-30
 * @Version: V1.0
 */
@RestController
@RequestMapping("/api/hosts")
@Slf4j
public class HostController {

    @Resource
    private IHostService hostService;

    /**
     * 分页列表查询
     *
     * @param host
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<IPage<Host>> queryPageList(Host host,
                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                             HttpServletRequest req) {
        LambdaQueryWrapper<Host> queryWrapper = new LambdaQueryWrapper<Host>();
        Page<Host> page = new Page<Host>(pageNo, pageSize);
        IPage<Host> pageList = hostService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param host
     * @return
     */
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody Host host) {
        hostService.save(host);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param host
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody Host host) {
        hostService.updateById(host);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestBody Map<String, String> requestData) {
        String id = requestData.get("id");
        hostService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<Host> queryById(@RequestParam(name = "id", required = true) String id) {
        Host host = hostService.getById(id);
        if (host == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(host);
    }
}
